<template>
  <div class="order">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card :body-style="{ padding: '20px' }">
      <!-- 顶部搜索区域 -->
      <div style="width:500px;display:flex;">
        <el-input placeholder="请输入内容" clearable v-model="queryInfo.query">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="search"
          ></el-button>
        </el-input>
      </div>
      <!-- 表格内容区域 -->
      <el-table border :data="orderList" style="width: 100%">
        <el-table-column type="index" label="#" width="40"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="200">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column label="付款方式">
          <template v-slot="scope">
            <el-tag type="danger" v-if="scope.row.order_pay === '0'"
              >未付款</el-tag
            >
            <el-tag type="primary" v-else>{{
              orderPay[scope.row.order_pay]
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column label="下单时间">
          <template v-slot="scope">
            <div>
              {{ scope.row.create_time | formatTime }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="editOrder(scope.row)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-map-location"
              @click="adressEdit(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px"
      >
      </el-pagination>
    </el-card>
    <!-- 修改订单 -->
    <el-dialog title="修改订单状态" :visible.sync="dialogVisible" width="50%">
      <el-form ref="form" :model="editInfo" label-width="80px" :rules="rules">
        <el-form-item label="是否发货" prop="is_send">
          <el-select v-model="editInfo.is_send" placeholder="请选择发货状态">
            <el-option label="未发货" value="0"></el-option>
            <el-option label="已经发货" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款方式" prop="order_pay">
          <el-select v-model="editInfo.order_pay" placeholder="请选择付款方式">
            <el-option label="未支付" value="0"></el-option>
            <el-option label="支付宝" value="1"></el-option>
            <el-option label="微信" value="2"></el-option>
            <el-option label="银行卡" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单价格" prop="order_price">
          <el-input v-model="editInfo.order_price"></el-input>
        </el-form-item>
        <el-form-item label="订单编号" prop="order_number">
          <el-input v-model="editInfo.order_number"></el-input>
        </el-form-item>
        <el-form-item label="支付状态" prop="pay_status">
          <el-select v-model="editInfo.pay_status" placeholder="请选择付款方式">
            <el-option label="未支付" value="0"></el-option>
            <el-option label="已支付" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editNow">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 地址修改弹出框 -->
    <el-dialog title="修改地址" :visible.sync="adressDialogVisible" width="50%">
      <el-form ref="adressform" label-width="80px">
        <el-form-item label="省市区/县">
          <el-cascader
            v-model="selectKey"
            :props="cascaderProps"
            :options="city"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="order_number">
          <el-input v-model="editInfo.adress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="adressDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="editAdress">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import city from '../../assets/adress/city'
export default {
  data () {
    return {
      // 城市数据
      city: city,
      // 修改订单弹出框
      dialogVisible: false,
      //  地址修改弹出框
      adressDialogVisible: false,
      //  请求订单列表的参数
      queryInfo: {
        //  查询参数
        query: '',
        //  当前页码
        pagenum: 1,
        //  每页显示条数
        pagesize: 10
      },
      //  修改订单参数
      editInfo: {
        // 订单是否发货 1:已经发货，0:未发货
        is_send: '',
        // 订单支付方式 支付方式 0 未支付 1 支付宝 2 微信 3 银行卡
        order_pay: '',
        // 订单价格
        order_price: '',
        // 订单编号
        order_number: '',
        //  支付状态  0 未付款、1 已付款
        pay_status: ''
      },
      //  修改订单详细地址
      adress: '',

      //  订单id
      orderId: '',
      // 订单数据列表
      orderList: [],
      //  订单总数据
      total: 0,
      //  级联选择器配置项
      //  级联选择器配置对象
      cascaderProps: {
        label: 'label',
        value: 'value',
        children: 'children',
        expandTrigger: 'hover'
      },
      selectKey: [],
      // 付款方式
      orderPay: {
        '0': '未支付',
        '1': '支付宝',
        '2': '微信',
        '3': '银行卡'
      },
      // 修改订单表单验证
      rules: {
        is_send: [
          {
            required: true,
            message: '请选择发货状态',
            trigger: 'blur'
          }
        ],
        order_pay: [
          {
            required: true,
            message: '请选择支付方式',
            trigger: 'blur'
          }
        ],
        order_price: [
          {
            required: true,
            message: '请输入价格',
            trigger: 'blur'
          }
        ],
        order_number: [
          {
            required: true,
            message: '请输入订单编号',
            trigger: 'blur'
          }
        ],
        pay_status: [
          {
            required: true,
            message: '请选择支付状态',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    // 获取订单数据列表
    async getOrderList () {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取失败!')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 搜索按钮点击
    search () {
      this.queryInfo.pagenum = 1
      this.queryInfo.pagesize = 10
      this.getOrderList()
    },
    //  分页器尺寸改变事件
    handleSizeChange (size) {
      this.queryInfo.pagesize = size
      this.getOrderList()
    },
    //  分页器页码改变
    handleCurrentChange (num) {
      this.queryInfo.pagenum = num
      this.getOrderList()
    },
    // 订单修改
    editOrder (row) {
      this.dialogVisible = true
      this.editInfo.is_send = row.is_send === '否' ? '0' : '1'
      this.editInfo.order_pay = row.order_pay
      this.editInfo.order_price = row.order_price
      this.editInfo.order_number = row.order_number
      this.editInfo.pay_status = row.pay_status
      this.orderId = row.order_id
    },
    // 订单修改弹出框确定按钮点击
    editNow () {
      this.dialogVisible = false
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put(
          `orders/${this.orderId}`,
          this.editInfo
        )
        if (res.meta.status !== 201) return this.$message.error('修改失败')
        this.$message.success('修改成功~')
        this.getOrderList()
      })
    },
    //  地址修改
    adressEdit (row) {
      this.adressDialogVisible = true
    },
    //  确定修改地址
    editAdress () {
      this.adressDialogVisible = false
      this.$message.success('修改成功')
    }
  }
}
</script>
<style lang="less" scoped>
.el-table {
  margin-top: 20px;
}
</style>
